<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.autocomplete.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<link rel="stylesheet"  href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" type="text/css" media="all"/>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<script src="<?php echo URL::base() ?>media/js/jquery.json-2.4.js"></script>

<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-addon.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-zh-CN.js"></script>
<link href="<?php echo URL::base() ?>media/vendor/css/jquery-ui-timepicker-addon.min.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<script>

    $(document).ready(function () {
        $("#reference_date").datepicker();
    });

    function search_infos(){
        url="<?php echo URL::site() ?>newreport/currentstocks/index";
        var datas=serializeForm('requireform');
        location.replace(url+"?"+datas);
    }

    function create_infos(){
        url="<?php echo URL::site() ?>newreport/currentstocks/create";
        var datas=serializeForm('requireform');
        location.replace(url+"?"+datas);
    }

    function download_infos(){
        htmlEditor();
        url="<?php echo URL::site() ?>newreport/currentstocks/index";
        var datas=serializeForm('requireform');
        $.get(
            url+"?"+datas+'&download=1',
            function(data){
                if(data==='1qw'){
                    alert('数据量超过千万，请使用更短的时间筛选间隔！');
                    HideIframe();
                }else{
                    url="<?php echo url::base(TRUE);?>"+data;
                    downloadFile(url);
                    HideIframe();
                }
            }
        )
    }

    function downloadFile(url) {
        try{
            var elemIF = document.createElement("iframe");
            elemIF.src = url;
            elemIF.style.display = "none";
            document.body.appendChild(elemIF);
        }catch(e){
            alert(e);
        }
    }

    //获取指定form中的所有的<input>对象
    function getElements(formId) {
        var form = document.getElementById(formId);
        var elements = new Array();
        var tagElements = form.getElementsByTagName('input');
        for (var j = 0; j < tagElements.length; j++){
            elements.push(tagElements[j]);
        }
        var tagElements = form.getElementsByTagName('select');
        for (var j = 0; j < tagElements.length; j++){
            elements.push(tagElements[j]);
        }
        return elements;
    }

    //获取单个input中的【name,value】数组
    function inputSelector(element) {
        if (element.checked)
            return [element.id, element.value];
    }

    function input(element) {
        switch (element.type.toLowerCase()) {
            case 'submit':
            case 'hidden':
            case 'password':
            case 'text':
                return [element.id, element.value];
            case 'checkbox':
            case 'radio':
                return inputSelector(element);
            case 'select-one':
                return [element.id, element.value];
        }
        return false;
    }

    //组合URL
    function serializeElement(element) {
        var parameter = input(element);

        if (parameter) {
            var key = encodeURIComponent(parameter[0]);
            if (key.length == 0) return;

            if (parameter[1].constructor != Array)
                parameter[1] = [parameter[1]];

            var values = parameter[1];
            var results = [];
            for (var i=0; i<values.length; i++) {
                results.push(key + '=' + values[i]);
            }
            return results;
        }
    }

    //调用方法
    function serializeForm(formId) {
        var elements = getElements(formId);
        var queryComponents = new Array();
        for (var i = 0; i < elements.length; i++) {
//            alert(elements[i].id);
            var queryComponent = serializeElement(elements[i]);
            if (queryComponent)
                queryComponents.push(queryComponent);
        }
        return queryComponents.join('&');
    }



    function createIframe(){
//mask遮罩层
        var newMask=document.createElement("div");
        newMask.id="mDiv";
        newMask.style.position="absolute";
        newMask.style.zIndex="1";
        _scrollWidth=Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
        _scrollHeight=Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
// _scrollHeight = Math.max(document.body.offsetHeight,document.documentElement.scrollHeight);
        newMask.style.width=_scrollWidth+"px";
        newMask.style.height=_scrollHeight+"px";
        newMask.style.top="0px";
        newMask.style.left="0px";
        newMask.style.background="#33393C";
//newMask.style.background = "#FFFFFF";
        newMask.style.filter="alpha(opacity=40)";
        newMask.style.opacity="0.40";
        newMask.style.display='none';
        var objDiv=document.createElement("DIV");
        objDiv.id="div1";
        objDiv.name="div1";
        objDiv.style.width="480px";
        objDiv.style.height="200px";
        objDiv.style.left=(_scrollWidth-480)/2+"px";
        objDiv.style.top=(_scrollHeight-200)/2+"px";
        objDiv.style.position="absolute";
        objDiv.style.zIndex="2"; //加了这个语句让objDiv浮在newMask之上
        objDiv.style.display="none"; //让objDiv预先隐藏
        objDiv.innerHTML=' <div id="drag" style="position:absolute;height:20px;width:100%;z-index:10001;top:0; background-color:#0033FF;cursor:move ;" align="right"> <input type=button value="X" onclick="HideIframe(document.getElementById(\'mDiv\'),document.getElementById(\'div1\'));"/> </div>';
//更改了X按钮为触发关闭事件。
        objDiv.style.border="solid #0033FF 3px;";
        var frm=document.createElement("iframe");
        frm.id="ifrm";
        frm.name="ifrm";
        frm.style.position="absolute";
        frm.style.width="100%";
        frm.style.height=180;
        frm.style.top=20;
        frm.style.display='';
        frm.frameborder=0;
        objDiv.appendChild(frm);
// newMask.appendChild(objDiv); //问题出在这里：你把frame所在的div变成了 newMask的子元素，当newMask透明度更改时，当然会影响到frame
        document.body.appendChild(newMask);
        document.body.appendChild(objDiv);
        var objDrag=document.getElementById("drag");
        var drag=false;
        var dragX=0;
        var dragY=0;
        objDrag.attachEvent("onmousedown",startDrag);
        function startDrag(){
            if(event.button==1&&event.srcElement.tagName.toUpperCase()=="DIV"){
                objDrag.setCapture();
                objDrag.style.background="#0000CC";
                drag=true;
                dragX=event.clientX;
                dragY=event.clientY;
            }
        };
        objDrag.attachEvent("onmousemove",Drag);
        function Drag(){
            if(drag){
                var oldwin=objDrag.parentNode;
                oldwin.style.left=oldwin.offsetLeft+event.clientX-dragX;
                oldwin.style.top=oldwin.offsetTop+event.clientY-dragY;
                oldwin.style.left=event.clientX-100;
                oldwin.style.top=event.clientY-10;
                dragX=event.clientX;
                dragY=event.clientY;
            }
        };
        objDrag.attachEvent("onmouseup",stopDrag);
        function stopDrag(){
            objDrag.style.background="#0033FF";
            objDrag.releaseCapture();
            drag=false;
        };
    }
    function htmlEditor(){
        var frm=document.getElementById("ifrm");
        var objDiv=document.getElementById("div1");
        var mDiv=document.getElementById("mDiv");
        mDiv.style.display='';
        frm.contentWindow.document.designMode='off';
        frm.contentWindow.document.open();
        frm.contentWindow.document.close();
    }

    function HideIframe(){
        var mDiv=document.getElementById("mDiv");
        mDiv.style.display='';
        mDiv.style.display='none';
    }
</script>
<body onLoad="createIframe()">
<form id="requireform">
    <input id="checksign"  value="1" hidden="hidden">
    SKU&nbsp&nbsp&nbsp：<input id="sku" <?php if(isset($_GET['sku'])&&!empty($_GET['sku'])){ ?>
        value="<?php echo isset($_GET['sku'])?$_GET['sku']:''; ?>"
    <?php } ?> ><br/>
    日期&nbsp&nbsp&nbsp：<input id="reference_date" <?php if(isset($_GET['reference_date'])&&!empty($_GET['reference_date'])){ ?>
        value="<?php echo isset($_GET['reference_date'])?$_GET['reference_date']:''; ?>"
    <?php } ?>><br/>

    <input id="search" type="button" value="查询" class="btn btn-info" onclick="search_infos()">
    <input id="download" type="button" value="下载" class="btn btn-info" onclick="download_infos()">
    <input id="download" type="button" value="数据更新" class="btn btn-info" onclick="create_infos()">
</form>
<?php if(isset($infos)&&!empty($infos)){?>
    <table id="skuinfos" border="1">
        <thead style=" background-color:#4EA392; color:#fff;" >
        <?php foreach($title as $key=>$value){ ?>
            <th>
                <?php echo $value?>
            </th>
        <?php } ?>
        </thead>
        <tbody>
        <?php foreach($infos as $info){ ?>
            <tr>
                <td>
                    <?php echo isset($info['reference_date'])?
                        $info['reference_date']:'';?>
                </td>
                <td>
                    <?php echo isset($info['sku'])?
                        $info['sku']:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['onways'])->quantity)?
                        json_decode($info['onways'])->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['onways'])->cost)?
                        json_decode($info['onways'])->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['inventories'])->quantity)?
                        json_decode($info['inventories'])->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['inventories'])->cost)?
                        json_decode($info['inventories'])->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{10})?
                        json_decode($info['warehouses'])->{10}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{10})?
                        json_decode($info['warehouses'])->{10}->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{20})?
                        json_decode($info['warehouses'])->{20}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{20})?
                        json_decode($info['warehouses'])->{20}->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{30})?
                        json_decode($info['warehouses'])->{30}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{30})?
                        json_decode($info['warehouses'])->{30}->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{80})?
                        json_decode($info['warehouses'])->{80}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{80})?
                        json_decode($info['warehouses'])->{80}->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{110})?
                        json_decode($info['warehouses'])->{110}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['warehouses'])->{110})?
                        json_decode($info['warehouses'])->{110}->cost:'';?>
                </td>
                <?php
                $otherTotalQuantityW=0;
                $otherTotalCostW=0;
                foreach(json_decode($info['warehouses']) as $key=>$objw){
                    if(in_array($key,array(10,20,30,80,110))){
                        continue;
                    }
                    $otherTotalQuantityW+=$objw->quantity;
                    $otherTotalCostW+=$objw->cost;
                }?>
                <td>
                    <?php echo $otherTotalQuantityW;?>
                </td>
                <td>
                    <?php echo $otherTotalCostW;?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{10})?
                        json_decode($info['deliveries'])->{10}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{10})?
                        json_decode($info['deliveries'])->{10}->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{20})?
                        json_decode($info['deliveries'])->{20}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{20})?
                        json_decode($info['deliveries'])->{20}->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{100})?
                        json_decode($info['deliveries'])->{100}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{100})?
                        json_decode($info['deliveries'])->{100}->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{110})?
                        json_decode($info['deliveries'])->{110}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{110})?
                        json_decode($info['deliveries'])->{110}->cost:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{120})?
                        json_decode($info['deliveries'])->{120}->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['deliveries'])->{120})?
                        json_decode($info['deliveries'])->{120}->cost:'';?>
                </td>
                <?php
                $otherTotalQuantityD=0;
                $otherTotalCostD=0;
                foreach(json_decode($info['deliveries']) as $key=>$objw){
                    if(in_array($key,array(10,20,100,110,120))){
                        continue;
                    }
                    $otherTotalQuantityD+=$objw->quantity;
                    $otherTotalCostD+=$objw->cost;
                }?>
                <td>
                    <?php echo $otherTotalQuantityD;?>
                </td>
                <td>
                    <?php echo $otherTotalCostD;?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['stocks'])->quantity)?
                        json_decode($info['stocks'])->quantity:'';?>
                </td>
                <td>
                    <?php echo isset(json_decode($info['stocks'])->cost)?
                        json_decode($info['stocks'])->cost:'';?>
                </td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php if (isset($pagination)) echo $pagination; ?>
<?php } ?>
</body>